<template>
	<view>


		<view class="all">
			<view class="one">
				<view class="title">
					反馈分类
				</view>
				<view class="lists">
					<view class="list" :class="{active:index2==index}" v-for="(item,index) in list" :key="index"
						@click="feek(index)">
						{{item}}
					</view>
				</view>
			</view>
			<view class="one">
				<view class="title">
					反馈问题
				</view>
				<view class="body">
					<textarea class="text" cols="30" rows="10" v-model="wenti"></textarea>
				</view>
			</view>
			<view class="one">
				<view class="title">
					问题图片
				</view>
				<u-upload style="border: 1px solid #ccc;width: 90px; border-radius: 10px;margin: 0 5px;"
					:fileList="fileList5" @afterRead="afterRead" @delete="deletePic" name="5" multiple :maxCount="1"
					uploadIconColor="#000"></u-upload>
			</view>
			<view class="one">
				<view class="title">
					联系人姓名
				</view>
				<view class="info">
					<input type="text" v-model="name" placeholder="请输入姓名" />
				</view>
			</view>
			<view class="one">
				<view class="title">
					联系人电话
				</view>
				<view class="info">
					<input type="number" maxlength="11" v-model="phone" placeholder="请输入电话" />
				</view>
			</view>

		</view>
		<view class="last">
			<view class="cary" @click="baocun()">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				index2: 0,
				fileList5: [],
				name:"",
				phone:"",
				wenti:"",
				url2:""
			};
		},
		onLoad() {
			this.getfeek()
		},
		methods: {
			async postfeed(){
				const res = await this.$api('/api/user/feedback','post',{
					type:this.list[this.index2],
					problem:this.wenti,
					image:this.url2,
					name:this.name,
					phone:this.phone,
				}).then((res)=>{
					// console.log(res);
					if(res.data.code==1){
						console.log(res.data.data);
						uni.$u.toast(res.data.msg)
					}
				}).catch((err)=>{
					uni.$u.toast(err.data.msg)
				})
			},
			baocun(){
				console.log(this.name,this.phone,this.url2,this.wenti,this.list);
				console.log(this.list[this.index2]);
				this.postfeed()
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://admin.lykangshu.top/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							resolve(res)
							const ret = JSON.parse(res.data)
							if (ret.code == 1) {
								console.log(ret.data.url);
								this.url2 = ret.data.url
							}
						}
					});
				})
			},


			feek(i) {
				console.log(i);
				this.index2 = i
			},
			async getfeek() {
				const res = await this.$api('/api/user/feedback', 'get').then((res) => {
					console.log(res.data.data.feedback_cate);
					this.list = res.data.data.feedback_cate
				})
			}
		}

	}
</script>

<style lang="scss" scoped>
	.all {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding: 15px 15px 40px 15px;

		.one {
			.title {
				font-size: 16px;
				font-weight: bold;
				color: #333333;
				margin: 10px 0;
			}

			.lists {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.list {
					background: #FB774B;
					border-radius: 26px;
					opacity: .3;
					color: #fff;
					text-align: center;
					padding: 5px;
					font-size: 13px;
					margin: 5px;
				}

				.active {
					background: #FB774B;
					border-radius: 26px;
					opacity: 1;
					color: #fff;
					text-align: center;
					padding: 5px;
					font-size: 13px;
					margin: 5px;
				}
			}

			.body {
				width: 100%;
				border: 1px solid #ccc;
				box-sizing: border-box;
				padding: 15px;
				border-radius: 10px;

				.text {
					width: 100%;

					font-size: 12px;
					font-weight: 400;
					color: #999999;
				}
			}

			.info {
				background: #FFFFFF;
				border-radius: 10px;
				padding: 10px;

				input {
					font-size: 14px;
					font-weight: bold;
					color: #333333;
				}
			}
		}
	}

	.last {
		position: fixed;
		width: 100%;
		bottom: 0;
		padding: 10px 10px 20px 10px;
		background: #FFFFFF;
		box-shadow: 0px -2px 4px 1px rgba(0, 0, 0, 0.05);
		box-sizing: border-box;
		.cary{
			background: #FB774B;
			border-radius: 40px;
			padding: 10px 0;
			text-align: center;
			color: #fff;
			
			font-size: 16px;
			font-weight: bold;
		}
	}
</style>